package com.example.linyiquan.login.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.example.linyiquan.login.MyVolley;
import com.example.linyiquan.login.R;

import org.json.JSONObject;

import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by linyiquan on 17-4-10.
 */

public class RegisterActivity extends Activity implements View.OnClickListener {

    private EditText et_username;
    private EditText et_password;
    private Button bt_register;
    private SimpleDateFormat format;

    private String username;
    private String password;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);

        initView();
    }

    private void initView(){
        et_username= (EditText) findViewById(R.id.username);
        et_password= (EditText) findViewById(R.id.password);
        bt_register= (Button) findViewById(R.id.bt_register);
        bt_register.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        username = et_username.getText().toString();
        password = et_password.getText().toString();

        if (TextUtils.isEmpty(username)||TextUtils.isEmpty(password)){
            Toast.makeText(this, "用户名密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        getVolley();
    }

    private void getVolley() {
        MyVolley.init(this);
        MyVolley.post("http://120.76.96.34/weizhanjsp/user/register", null, new MyVolley.onResultListener() {
            @Override
            public void done(JSONObject result) {
                Log.d("APP",result.toString());
                getResult();
            }

            @Override
            public void fail(VolleyError error) {
                Log.d("APP",error.getMessage(),error);
            }
        });
    }

    protected Map<String, String> getResult(){
        format = new SimpleDateFormat("yyyy年mm月dd日 HH：mm：ss");
        Date date=new Date(System.currentTimeMillis());
        String time=format.format(date);

        HashMap<String, String> map = new HashMap<>();

        map.put("username", username);
        map.put("password", password);
        map.put("birthday",time);
        map.put("clientType","1");
        return map;
    }
}
